package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
@Autowired
private DishMapper dishMapper;
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private SetmealMapper setmealMapper;
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //增加购物车的逻辑
        ///*dishFlavor	string	非必须		口味
        //dishId	integer	非必须		菜品id
        //format: int64
        //
        //setmealId	integer	非必须		套餐id
        //format: int64*/
        //每次添加要么是一个菜，要么是一个套餐，
        ShoppingCart cart = shoppingCartMapper.select(shoppingCartDTO);
        if (cart!=null){
            //如果之前有则加一
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
            return;
        }

        //没有则新插入
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        if (shoppingCartDTO.getDishId()!=null){
            Dish dish = dishMapper.select(shoppingCartDTO.getDishId());
            shoppingCart.setDishId(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setAmount(dish.getPrice());

        }
        else {
            Setmeal setmeal=setmealMapper.selectById(shoppingCartDTO.getSetmealId());
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> getCart() {
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.getcarts(currentId);

    }

    @Override
    public void delete() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //删除购物车中一个商品逻辑，
        //先去数据库中查，看有没有，有则减一，没有不做任何操作
        ShoppingCart cart = shoppingCartMapper.select(shoppingCartDTO);
        if (cart!=null){
            //减一，如果数量为零，则从库中删除。
            if (cart.getNumber()-1==0)
                shoppingCartMapper.deleteByCartId(cart.getId());
            else {
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.update(cart);
            }
        }
        else {
            return;
        }
    }
}
